package ext.vpde.cadvaulting.util;

// java imports
import java.io.*;
import java.net.*;
import java.util.*;


public class FtpUtil
{
// java ext.vpde.cadvaulting.util.FtpUtil ftp.archergrey.com cpeabody@archergrey.com agcp834 /FTP/Windchill/pdm-300-0001.drw.pdf D:\temp\pdm-300-0001.drw.pdf
// java ext.vpde.cadvaulting.util.FtpUtil ftp.archergrey.com cpeabody%40archergrey.com agcp834 pdm-300-0001.drw.pdf pdm-300-0001.drw.pdf
//ftp://cpeabody%40archergrey.com:agcp834@ftp.archergrey.com;type=d
   public static void main(String args[]){
	   try{
		   String ftpServer = args[0];
		   String user = args[1];
		   String pwd = args[2];
		   //String fileName = args[3];
		   //File source = new File(args[4]);
		   //if (!source.exists()) System.out.println("*** THE THING AINT THERE!!! ***");
		   //System.out.println("ftpserver: " + ftpServer + "\nuser: " + user + "\npwd: " + pwd + "\nfileName: " + fileName + "\nsource: " + source.getName() );

		   ftpDir(ftpServer, user, pwd);
	   }catch(Exception e){e.printStackTrace();}
   }

   /**
    * If the client needs to connect to the server through a proxy, this method sets the JVM FTP proxy settings.
    *
    * @param proxyHost Host name of proxy.
    * @param proxyPort Port number of proxy host.
    */
   public static void setProxy(String proxyHost, int proxyPort) {
       if (proxyHost != null) {
           System.getProperties().put("ftpProxySet", "true");
           System.getProperties().put("ftpProxyHost", proxyHost);
           System.getProperties().put("ftpProxyPort", ""+proxyPort);
       }
   }//setProxy()

   /**
    * Upload a file to a FTP server. A FTP URL is generated with the following syntax:
    * <code>ftp://user:password@host:port/urlpath;type=i</code>.
    *
    * @param ftpServer FTP server address.
    * @param user Optional user name to login.
    * @param pwd Optional password for <i>user</i>.
    * @param fileName Destination file name on FTP server (with optional preceeding relative path, e.g. one/two/three.txt).
    * @param source Source file to upload.
    * @throws Exception on error.
    */
   public static void upload(String ftpServer, String user, String pwd, String fileName, File source) throws MalformedURLException,
           IOException {

       if (ftpServer != null && fileName != null && source != null) {
           StringBuffer sb = new StringBuffer("ftp://");
           if (user != null && pwd != null) {
               sb.append(user);
               sb.append(':');
               sb.append(pwd);
               sb.append('@');
           }
           sb.append(ftpServer);
           sb.append('/');
           sb.append(fileName);
           sb.append(";type=i"); //a=ASCII mode, i=image (binary) mode, d= file directory listing
           System.out.println(sb.toString());

           BufferedInputStream bis = null;
           BufferedOutputStream bos = null;
           try {
               URL url = new URL(sb.toString());
               URLConnection urlc = url.openConnection();

               bos = new BufferedOutputStream(urlc.getOutputStream());
               bis = new BufferedInputStream(new FileInputStream(source));

               int i;
               while ((i = bis.read()) != -1) {
                   bos.write(i);
               }
           } finally {
               if (bis != null) { try { bis.close(); } catch (IOException ioe) { /* ignore*/ } }
               if (bos != null) { try { bos.close(); } catch (IOException ioe) { /* ignore*/ } }
           }
       }
   }//upload()

   /**
   	 * Download a file from a FTP server. A FTP URL is generated with the following syntax:
    * <code>ftp://user:password@host:port/filePath;type=i</code>.
   	 *
   	 * @param ftpServer FTP server address (with optional port ':<port>').
   	 * @param user Optional user name to login.
   	 * @param pwd Optional password for <i>user</i>.
   	 * @param fileName Name of file to download (with optional preceeding relative path, e.g. one/two/three.txt).
   	 * @param destination Destination file to save.
                     * @throws Exception on error.
    */
   	public static void download(String ftpServer, String user, String pwd, String fileName, File destination) throws MalformedURLException,
           IOException {

       if (ftpServer != null && fileName != null && destination != null) {
           StringBuffer sb = new StringBuffer("ftp://");
           if (user != null && pwd != null) {
               sb.append(user);
               sb.append(':');
               sb.append(pwd);
               sb.append('@');
           }
           sb.append(ftpServer);
           sb.append('/');
           sb.append(fileName);
           sb.append(";type=i"); //a=ASCII mode, i=image (binary) mode, d= file directory listing

           BufferedInputStream bis = null;
           BufferedOutputStream bos = null;
           try {
               URL url = new URL(sb.toString());
               URLConnection urlc = url.openConnection();

               bis = new BufferedInputStream(urlc.getInputStream());
               bos = new BufferedOutputStream(new FileOutputStream(destination.getName()));

               int i;
               while ((i = bis.read()) != -1) {
                   bos.write(i);
               }
           } finally {
               if (bis != null) { try { bis.close(); } catch (IOException ioe) { /* ignore*/ } }
               if (bos != null) { try { bos.close(); } catch (IOException ioe) { /* ignore*/ } }
           }
       }
   }//download()
   	public static String ftpDir(String ftpServer, String user, String pwd) throws MalformedURLException,
           IOException {
       StringBuffer output = new StringBuffer();

       if (ftpServer != null) {
           StringBuffer sb = new StringBuffer("ftp://");
           if (user != null && pwd != null) {
               sb.append(user);
               sb.append(':');
               sb.append(pwd);
               sb.append('@');
           }
           sb.append(ftpServer);
           sb.append("/;type=d"); //a=ASCII mode, i=image (binary) mode, d= file directory listing

           BufferedInputStream bis = null;
           BufferedOutputStream bos = null;
           try {
               URL url = new URL(sb.toString());
               URLConnection urlc = url.openConnection();

               DataInputStream dis = new DataInputStream(urlc.getInputStream());
               String inputLine;

               while ((inputLine = dis.readLine()) != null) {
				   output.append(inputLine);
                   System.out.println(inputLine);
               }
               dis.close();

           } catch (MalformedURLException me) {
			   System.out.println("MalformedURLException: " + me);
		   } catch (IOException ioe) {
			   System.out.println("IOException: " + ioe);
		   }
       }
       return output.toString();
   }//sendCMD()
}